package main

import (
	"FenBuShiProject/grades"
	"FenBuShiProject/mylog"
	"FenBuShiProject/registry"
	"FenBuShiProject/service"
	"context"
	"fmt"
	stlog "log"
)

func main() {

	host, port := "localhost", "6000"
	serviceAddress := fmt.Sprintf("http://%s:%s", host, port)
	r := registry.Registration{
		ServiceName:      registry.GradingService,
		ServiceURL:       serviceAddress,
		RequiredServices: []registry.ServiceName{registry.LogService},
		ServiceUpdateURL: serviceAddress + registry.ServiceUpdateURL,
		HeartbeatURL:     serviceAddress + registry.HeartbeatURL,
	}
	ctx, err := service.Start(
		context.Background(),
		host,
		port,
		r,
		grades.RegisterHandlers,
	)

	if err != nil {
		stlog.Fatal(err)
	}
	//启动所有依赖的客户端
	for i := range r.RequiredServices {
		if providerURLs, err := registry.GetProviderURLs(r.RequiredServices[i]); err == nil {
			for j := range providerURLs {
				fmt.Printf("%s found at %s ", r.RequiredServices[i], providerURLs[j])
				mylog.SetClientLogger(providerURLs[j], r.ServiceName)
			}
		}
	}

	<-ctx.Done() //Done返回一个channel，这里让channel输出，也即cancel后channel关闭才能输出，就是一个阻塞。
	fmt.Println("Shutting down service")
}
